home *** CD-ROM | disk | FTP | other *** search
- /*-------------------------------------------------------------------------------------
- *
- * Simple Sample Application Framework
- *
- * ©1991 Apple Computer
- *
- -------------------------------------------------------------------------------------*/
- /*
- * events.c -- main event loop and basic event handling
- *
- * change history:
- *
- * SJF 11/6/91 1.0d1 initial coding
- *
- */
-
- #include <AppleEvents.h>
-
- #include "const.h"
- #include "mytypes.h"
- #include "mymenus.h"
- #include "globals.h"
- #include "utils.h"
- #include "windowstuff.h"
- #include "commands.h"
- #include "aevt.h"
-
- #include "events.h"
-
- /* main event loop */
-
- void MainLoop(void)
- {
- EventRecord ev;
- Boolean gotEvent;
-
- while (!gDone) {
- if (gHasWaitNextEvent)
- gotEvent = WaitNextEvent(everyEvent,&ev,0/*SleepTime()*/,nil);
- else {
- gotEvent = GetNextEvent(everyEvent,&ev);
- SystemTask();
- }
-
- ProcessEvent(&ev);
- }
- }
-
-
- /* calculate how long to "wait" */
-
- long SleepTime(void)
- {
- if (gInBackground)
- return kSleepBackground;
- else
- return kSleepForeground;
- }
-
-
- /* set cursor shape depending on mouse location */
-
- void HandleFixCursor(Point where,RgnHandle theRgn)
- {
- }
-
-
- /* idle time processing */
-
- void HandleIdle(WindowPtr window)
- {
- WindowPtr iWindow;
-
- for (iWindow=FrontWindow(); IsAppWindow(iWindow); iWindow=(WindowPtr)((WindowPeek)iWindow)->nextWindow)
- SendWindowMessage(iWindow,kIdleMessage,nil);
- }
-
-
- /* process an event gotten by MainLoop() */
-
- void ProcessEvent(EventRecord *ev)
- {
- void *returnResult;
-
- returnResult = SendWindowMessage(FrontWindow(),kEventMessage,ev);
- if (returnResult!=nil)
- return;
-
- switch (ev->what) {
- case mouseDown:
- HandleMouseDowns(ev);
- break;
- case keyDown:
- case autoKey:
- HandleKeyDowns(ev);
- break;
- case updateEvt:
- HandleUpdates((WindowPtr)ev->message);
- break;
- case activateEvt:
- HandleActivates(ev);
- break;
- case osEvt:
- HandleSREvt(ev->where,ev->message);
- break;
- case kHighLevelEvent:
- DoHighLevelEvent(ev);
- break;
- case nullEvent:
- HandleIdle(FrontWindow());
- break;
- }
- }
-
-
- /* Handles suspend and resume events */
-
- void HandleSREvt(Point where,long message)
- {
- extern NMRec *gNotify;
- extern Boolean gInBackground;
- unsigned long whatMessage;
-
- whatMessage = message >> 24;
-
- if (whatMessage==suspendResumeMessage) {
- if ((message & 1) != 0) {
- gInBackground = false;
- SetCursor(&qd.arrow);
- if (FrontWindow()) {
- HiliteWindow(FrontWindow(),true);
- DoActivate(FrontWindow(),true);
- }
- }
- else if (FrontWindow()) {
- gInBackground = true;
- HiliteWindow(FrontWindow(),false);
- DoDeActivate(FrontWindow(),true);
- }
- }
- else if ((whatMessage&mouseMovedMessage)==mouseMovedMessage)
- HandleFixCursor(where,gCursorRgn);
- }
-
-
- /* Handles activate and deactivate events for a window */
-
- void HandleActivates(EventRecord *ev)
- {
- if ((ev->modifiers & activeFlag) != 0) {
- DoActivate((WindowPtr)ev->message,((ev->modifiers & 0x0002) != 0));
- }
- else {
- DoDeActivate((WindowPtr)ev->message,((ev->modifiers & 0x0002) != 0));
- }
- }
-
-
- /* Handles activate events for a window */
-
- void DoActivate(WindowPtr window,Boolean chFlag)
- {
- SendWindowMessage(window,kActivateMessage,&chFlag);
- }
-
-
- /* Handles deactivate events for a window */
-
- void DoDeActivate(WindowPtr window,Boolean chFlag)
- {
- SendWindowMessage(window,kDeactivateMessage,&chFlag);
- }
-
-
- /* handles update events for a window */
-
- void HandleUpdates(WindowPtr window)
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(window);
-
- BeginUpdate(window); // hack alert!
- EndUpdate(window);
-
- SendWindowMessage(window,kUpdateMessage,nil);
- SetPort(savePort);
- }
-
-
- /* handles program keydowns */
-
- void HandleKeyDowns(EventRecord *ev)
- {
- short theChar;
- WindowPtr window;
-
- theChar = ev->message & charCodeMask;
- if ((ev->modifiers & cmdKey) != 0)
- DoMenuCommand(MenuKey(theChar));
- else if (IsAppWindow(window=FrontWindow()))
- SendWindowMessage(window,kKeyMessage,&theChar);
- }
-
-
- /* handles mouse down events for a window */
-
- void HandleMouseDowns(EventRecord *ev)
- {
- WindowPtr window;
- short part;
-
- part = FindWindow(ev->where,&window);
-
- switch (FindWindow(ev->where,&window)) {
- case inMenuBar:
- DoMenuCommand(MenuSelect(ev->where));
- break;
- case inSysWindow:
- SystemClick(ev,window);
- break;
- case inDrag:
- DoDrag(window,ev->where);
- break;
- case inGrow:
- DoGrow(window,ev->where);
- break;
- case inGoAway:
- if (TrackGoAway(window,ev->where)) {
- CommCloseWindow(window);
- }
- break;
- case inZoomIn:
- case inZoomOut:
- if (TrackBox(window,ev->where,FindWindow(ev->where,&window)))
- DoZoom(window,FindWindow(ev->where,&window));
- break;
- case inContent:
- DoContentClick(window,ev);
- break;
- }
- }
-
-
- /* handles window drag events */
-
- void DoDrag(WindowPtr window,Point globMouse)
- {
- Rect dragRect = kWindowDragLimits;
-
- DragWindow(window,globMouse,&dragRect);
- SendWindowMessage(window,kResizeMessage,&window->portRect);
- SetPort(window);
- }
-
-
- /* handles window grow events */
-
- void DoGrow(WindowPtr window,Point globMouse)
- {
- long newSize;
- Rect windLimits = kWindowGrowLimits;
- Rect oldSize;
- GrafPtr tempPort;
-
- oldSize = window->portRect;
- if ((newSize = GrowWindow(window,globMouse,&windLimits)) != 0) {
- GetPort(&tempPort);
- SetPort(window);
- SizeWindow(window,LoWord(newSize),HiWord(newSize),true);
- SendWindowMessage(window,kResizeMessage,&oldSize);
- InvalRect(&window->portRect);
- SetPort(tempPort);
- }
- }
-
-
- /* handles window zooms */
-
- void DoZoom(WindowPtr window,short part)
- {
- GrafPtr tempPort;
-
- GetPort(&tempPort);
- SetPort(window);
- ZoomWindow(window,part,true);
- SendWindowMessage(window,kResizeMessage,nil);
- InvalRect(&window->portRect);
- SetPort(tempPort);
- }
-
-
- /* handles a click in a window content region */
-
- void DoContentClick(WindowPtr window,EventRecord *ev)
- {
- GrafPtr savePort;
-
- if (window != FrontWindow()) {
- SelectWindow(window);
- return;
- }
-
- SendWindowMessage(window,kClickMessage,ev);
- }
-
-
- /* handles menu commands */
-
- void DoMenuCommand(long mResult)
- {
- short selItem,selMenu,temp;
- Str255 name;
- Boolean flag;
- GrafPtr tempPort;
- MenuHandle theMenu;
-
- selItem = LoWord(mResult);
- selMenu = HiWord(mResult);
- switch (selMenu) {
- case kAppleMenu:
- if (selItem>2) {
- GetPort(&tempPort);
- SetCursor(&qd.arrow);
- theMenu = GetMHandle(kAppleMenu);
- GetItem(theMenu,selItem,name);
- temp = OpenDeskAcc(name);
- SetPort(tempPort);
- }
- else CommAbout();
- break;
- case kFileMenu:
- switch (selItem) {
- case kNewItem:
- CommNew();
- break;
- case kOpenItem:
- CommOpen();
- break;
- case kCloseItem:
- CommCloseWindow(FrontWindow());
- break;
- case kSaveItem:
- CommSaveFile(FrontWindow());
- break;
- case kSaveAsItem:
- CommSaveAsFile(FrontWindow());
- break;
- case kPageSetupItem:
- CommPageSetup(FrontWindow());
- break;
- case kPrintItem:
- CommPrint(FrontWindow());
- break;
- case kQuitItem:
- gDone = true;
- break;
- }
- break;
- case kEditMenu:
- if (!(SystemEdit(selItem-1)))
- CommEdit(FrontWindow(),selItem);
- break;
- }
- HiliteMenu(0);
- }
-
-
- OSErr HandleOpenDoc(FSSpec *fSpec)
- {
- LoOpen(fSpec);
- return noErr;
- }
-
-
- OSErr HandlePrintDoc(FSSpec *fSpec)
- {
- }
-
-
- void ExitProgram(void)
- {
- WindowPtr window;
-
- while (window=FrontWindow())
- CommCloseWindow(window);
- }
-
-